<template>
    <fieldset class="field-container">
        <input type="text" placeholder="搜索..." class="field" id="input" @change="inputSearch"/>
        <div class="icons-container">
            <div class="icon-search" @click="inputSearch"></div>
            <div class="icon-close">
                <div class="x-up"></div>
                <div class="x-down"></div>
            </div>
        </div>
    </fieldset>
</template>

<script>
export default {
    methods: {
        // 输入框搜索
        inputSearch() {
            const question = this.$("#input").val()

            if (!question) return this.$message({
                message: '请输入要搜索的药品名',
                type: 'warning'
            })

            this.request.post("/searchEngines/searchRecognition/", {
                question: question
            }).then((response) => {
                const status = response["code"] === 200

                if (status) {
                    this.$store.commit("PhotoResultModule/changeIsLoading", false)

                    const searchResult = response["data"]["searchResult"]
                    this.$store.commit("PhotoResultModule/changePageInfo", {
                        searchType: "药品说明",
                        searchResult: searchResult,
                    })
                } else {
                    setTimeout(() => {
                        this.$router.push("/searchView/")
                    }, 1500)
                }
            })

            // 跳转搜索内容页面
            this.$store.commit("PhotoResultModule/init")
            this.$store.commit("PhotoResultModule/changeAllowRouter", true)
            this.$router.push("/searchResultView/")
        }
    }
}

</script>

<style lang="less" scoped>

@-webkit-keyframes spin {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}

@-webkit-keyframes color-1 {
    0% {
        background-color: #EB73B9;
    }
    100% {
        background-color: #17EAD9;
    }
}

@keyframes color-1 {
    0% {
        background-color: #EB73B9;
    }
    100% {
        background-color: #17EAD9;
    }
}

.field-container {
    position: relative;
    border: 0;
    width: 100%;
    height: 55px;
    display: flex;
    align-items: center;
    z-index: 1;
}

.icons-container {
    position: absolute;
    right: 20px;
    width: 35px;
    height: 35px;
    overflow: hidden;
}

.icon-close {
    position: absolute;
    top: 2px;
    left: 2px;
    width: 75%;
    height: 75%;
    opacity: 0;
    cursor: pointer;
    transform: translateX(-200%);
    border-radius: 50%;
    transition: opacity 0.25s ease, transform 0.43s cubic-bezier(0.694, 0.048, 0.335, 1);
}

.icon-close:before {
    content: "";
    border-radius: 50%;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    border: 2px solid transparent;
    border-top-color: var(--color);
    border-left-color: var(--color);
    border-bottom-color: var(--color);
    transition: opacity 0.2s ease;
}

.icon-close .x-up {
    position: relative;
    width: 100%;
    height: 50%;
}

.icon-close .x-up:before {
    content: "";
    position: absolute;
    bottom: 2px;
    left: 3px;
    width: 50%;
    height: 2px;
    background-color: var(--color);
    transform: rotate(45deg);
}

.icon-close .x-up:after {
    content: "";
    position: absolute;
    bottom: 2px;
    right: 0;
    width: 50%;
    height: 2px;
    background-color: var(--color);
    transform: rotate(-45deg);
}

.icon-close .x-down {
    position: relative;
    width: 100%;
    height: 50%;
}

.icon-close .x-down:before {
    content: "";
    position: absolute;
    top: 5px;
    left: 4px;
    width: 50%;
    height: 2px;
    background-color: var(--color);
    transform: rotate(-45deg);
}

.icon-close .x-down:after {
    content: "";
    position: absolute;
    top: 5px;
    right: 1px;
    width: 50%;
    height: 2px;
    background-color: var(--color);
    transform: rotate(45deg);
}

.is-type .icon-close:before {
    opacity: 1;
    -webkit-animation: spin 0.85s infinite;
    animation: spin 0.85s infinite;
}

.is-type .icon-close .x-up:before, .is-type .icon-close .x-up:after {
    -webkit-animation: color-1 0.85s infinite;
    animation: color-1 0.85s infinite;
}

.is-type .icon-close .x-up:after {
    -webkit-animation-delay: 0.3s;
    animation-delay: 0.3s;
}

.is-type .icon-close .x-down:before, .is-type .icon-close .x-down:after {
    -webkit-animation: color-1 0.85s infinite;
    animation: color-1 0.85s infinite;
}

.is-type .icon-close .x-down:before {
    -webkit-animation-delay: 0.2s;
    animation-delay: 0.2s;
}

.is-type .icon-close .x-down:after {
    -webkit-animation-delay: 0.1s;
    animation-delay: 0.1s;
}

.icon-search {
    position: relative;
    top: 5px;
    left: 8px;
    width: 50%;
    height: 50%;
    opacity: 1;
    border-radius: 50%;
    border: 3px solid var(--color);
    transition: opacity 0.25s ease, transform 0.43s cubic-bezier(0.694, 0.048, 0.335, 1);
}

.icon-search:after {
    content: "";
    position: absolute;
    bottom: -9px;
    right: -2px;
    width: 4px;
    border-radius: 3px;
    transform: rotate(-45deg);
    height: 10px;
    background-color: var(--color);
}

.field {
    width: 100%;
    height: 100%;
    padding: 10px 20px;
    background: white;
    border-radius: 5px;
    transition: all 0.4s ease;
    box-sizing: border-box;
    border: 2px solid var(--color);
    font-size: 18px;
}

.field:focus {
    outline: none;
}

.field:focus + .icons-container .icon-close {
    opacity: 1;
    transform: translateX(0);
}

.field:focus + .icons-container .icon-search {
    opacity: 0;
    transform: translateX(200%);
}
</style>